<template>
  <div class="order_query">
    <p class="header">统筹单在线查验</p>
    <div class="content">
      <div class="query_icon">
        <img src="/query.png" alt="" />
      </div>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        class="demo-ruleForm"
        status-icon
      >
        <el-form-item label="" prop="plateNumber">
          <el-input
            placeholder="请输入车牌号码"
            v-model.trim="ruleForm.plateNumber"
            maxlength="16"
          />
        </el-form-item>
        <el-form-item prop="vin">
          <el-input
            placeholder="请输入车辆车架号后4位"
            v-model.trim="ruleForm.vin"
            maxlength="8"
          />
        </el-form-item>
        <el-form-item class="submit_form_item">
          <el-button
            size="large"
            style="width: 100%"
            type="primary"
            @click="submitForm(ruleFormRef)"
          >
            进行查验
          </el-button>
        </el-form-item>
        <el-form-item class="submit_form_item">
          <el-button
            size="large"
            style="width: 100%"
            @click="resetForm(ruleFormRef)"
          >
            取消查验</el-button
          >
        </el-form-item>
      </el-form>
      <p class="tips">
        备注:请输入相匹配的车牌号码与车架号(输入车架号后4位匹配即可)，若查验成功，将展示统筹单信息，否则，说明该车牌号与车架号不匹配或未查找到相应统筹单。
      </p>
    </div>
    <div class="footer"></div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import axios from "@/request";

const ruleFormRef = ref();
const ruleForm = reactive({
  plateNumber: "",
  vin: "",
});
const router = useRouter();

const rules = reactive({
  plateNumber: [{ required: true, message: "请输入车牌号", trigger: "blur" }],
  vin: [{ required: true, message: "请输入车架号后4位", trigger: "blur" }],
});

const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      handleQuery();
    } else {
      console.log("error submit!", fields);
    }
  });
};
// 查询
const handleQuery = () => {
  axios
    .get(
      `/api/contract/search?carNumber=${ruleForm.plateNumber}&vin=${ruleForm.vin}`
    )
    .then((result) => {
      const { status, data } = result;
      if (status === 200) {
        if (data.length > 0) {
          let orderId = data[0].orderId;
          router.push(`/orderDetails?id=${orderId}`);
        } else {
          ElMessage.error("没有查询到信息!");
        }
      } else {
        ElMessage.error("没有查询到信息!");
      }
    })
    .catch((err) => {});
};

const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};

onMounted(() => {
  document.title = "服务单查验";
});
</script>

<style scoped>
.order_query {
}

.order_query .header {
  text-align: center;
  border-bottom: 1px solid #e6e6e6;
  line-height: 40px;
  font-weight: bold;
}

.order_query .content {
  margin: 30px 40px;
}

.order_query .content .query_icon {
  text-align: center;
  margin-bottom: 30px;
}

.tips {
  color: #999;
}
</style>